+function ($) {
    "use strict";
    var upload, defaults = {
        imgList: [],
        url: 'https://devapi.mcloudsoft.cn/common/v1/Image/SmallUpload',
        edit: true,
        max: 9,
        multiple: true,
        accept: ['image/jpg', 'image/jpeg', 'image/png', 'image/gif'],
        onRemoveSuccess: undefined,
        onUploadSuccess: undefined,
        onUploadFail: undefined,
        size: 1,//M
        tpl: '<div class="page-bd-15">\
        <div class="weui-uploader">\
            <div class="weui-uploader__bd">\
                <ul class="weui-uploader__files">\
                {{#imgList}}\
                <li data-item="{{this}}"  class="weui-uploader__file" style="margin-top:9px;position:relative;background-image:url({{this}})"><input value="{{this}}"  type="hidden"  name="files" />\
                <span  style="position: absolute;top: -.7em;right: -.4em;"><i style="color:#FA5151;font-size: 22px;" class="weui-icon-clear"></i></span>\
                </li>\
                {{/imgList}}\
                </ul>\
                <div style="margin-top:9px;" class="weui-uploader__input-box">\
                <input class="weui-uploader__input" accept="image/*" multiple="multiple" type="file">\
                </div>\
            </div>\
        </div>',
        readonlytpl: ' <div class="page-bd-15">\
        <div class="weui-uploader">\
            <div class="weui-uploader__bd">\
                <ul class="weui-uploader__files">\
                {{#imgList}}\
                <li data-item="{{this}}"  class="weui-uploader__file" style="margin-top:9px;position:relative;background-image:url({{this}})"><input value="{{this}}"  type="hidden"  name="files" />\
                </li>\
                {{/imgList}}\
                </ul>\
            </div>\
        </div>',
    }
    $.fn.lobsterupload = function (params) {
        return this.each(function () {
            params = $.extend({}, defaults, params);
            if (params.max <= params.imgList.length) {
                $(".weui-uploader__input-box").hide();
            }
            upload = $(this);
            if (params.edit)
                upload.html($.t7.compile(params.tpl)(params));
            else
                upload.html($.t7.compile(params.readonlytpl)(params)); 
            upload.on("change", "[type='file']", function (event) {
                // $.showLoading("上传中");
                var files = event.target.files;
                // 如果没有选中文件，直接返回
                if (files.length === 0) {
                    return;
                }
                if (files.length > params.max) {
                    $.toast("上传数量不能超过" + params.max + "张", 'forbidden');
                    return;
                }
                for (var i = 0; i < files.length; i++) {
                    var file = files[i];
                    if (params.accept.indexOf(file.type) === -1) {
                        $.toast('该类型不允许上传', 'forbidden');
                        continue;
                    }
                    if (file.size > params.size * 1024 * 1024) {
                        $.toast('图片太大，不允许上传', 'forbidden');
                        continue;
                    }
                    var formData = new FormData();
                    formData.append("userfile", file);
                    $.ajax({
                        type: 'POST',
                        url: params.url,
                        data: formData,
                        cache: false,
                        processData: false,
                        contentType: false,
                        success: function (res) {
                            $("[type='file']").parent().prev().append(' <li data-item="' + res.data.fullSrc + '" class="weui-uploader__file" style="margin-top:9px;position:relative;background-image:url(' + res.data.fullSrc + ')"><input value=' + res.data.fullSrc + ' type="hidden"  name="files" />\
                            <span style="position: absolute;top:-.7em;right: -.4em;"><i style="color:#FA5151;font-size: 22px;" class="weui-icon-clear"></i></span>\
                            </li>');
                            if (params.max <= upload.find("li").length) {
                                $(".weui-uploader__input-box").hide();
                            }
                            params.onUploadSuccess && typeof params.onUploadSuccess == "function" && params.onUploadSuccess(res.data.fullSrc);
                        }, fail: function (res) {
                            params.onUploadFail && typeof params.onUploadFail == "function" && params.onUploadFail(res);
                        }
                    });
                }
            })
            upload.on("click", "span", function (event) {
                var obj = $(this).parent();
                var item = obj.data("item");
                $.confirm('您确定要删除吗?', '确认删除?', function () {
                    $(obj).remove();
                    if (upload.find("li").length >= params.max) {
                        $(".weui-uploader__input-box").hide();
                    } else {
                        $(".weui-uploader__input-box").show();
                    }
                    params.onRemoveSuccess && typeof params.onRemoveSuccess == "function" && params.onRemoveSuccess(item);
                });
                return false;
            })
        })
    };
}($);